package com.m3958.firstgwt.client.nativemethod;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.event.shared.EventBus;
import com.google.inject.Inject;
import com.smartgwt.client.util.SC;

public class NativeUtils {
	
	@Inject
	private EventBus eventBus;
	
	
	public JsArrayString getJsArrayString(String...ss){
		JsArrayString jsas = JsArrayString.createArray().cast();
		for(String s : ss){
			jsas.push(s);
		}
		return jsas;
	}
	
		private void showIndicator(String s){
			if(s == null || s.isEmpty()){
				SC.showPrompt("");
			}else{
				SC.showPrompt(s);
			}
		}
		
		private void hideIndicator(){
			SC.clearPrompt();
		}
	 
	 private Map<String, Boolean> scriptStatus = new HashMap<String, Boolean>();
	 
	 public void putScriptStatus(String key,boolean b){
		 scriptStatus.put(key, b);
	 }
	 
	 public boolean getScriptStatus(String key){
		 if(scriptStatus.containsKey(key)){
			 return scriptStatus.get(key);
		 }else{
			 return false;
		 }
	 }
	 public final native void addMessageClickListener()/*-{
	 		$wnd.Y.delegate('click',function(e){
				e.preventDefault();
				var uuid = this.get('id');
				(function(){
					function changeLoginStatus(){
						if(ns.logined){
							loginBar.setContent('<span>' + ns.user.loginName +  '</span> <a href="/openid.html#tab=3">修改</a> | <a href="/notrpclogin?logout=true">登出</a>');
						}
					}
					var tH = {
							success: function(id, o, args) {
									   var res = $wnd.Y.JSON.parse(o.responseText);
									   if(res.response.status == 0){
										   alert("成功");
									   }else{
										   alert('失败！');
									   }
									 },
							failure: function(id, o, args) {
										alert("发生未知故障！");
									 }
	
						};
					var cfg = {
							method:'POST',
							data:'uuid=' + uuid,
							on: {
								success: tH.success,
								failure: tH.failure
							},
							context: tH
						};
					
					$wnd.Y.io('/tokenconsume', cfg);		
				})();
		},'body','a.m3958-confirm-token');
	}-*/;
	 

	
	public  final native void hideKindeditorIframe()/*-{
		$wnd.Y.all('iframe.ke-iframe').each(function(node){
			node.hide();
		});
	}-*/;
	
	public  final native void showKindeditorIframe()/*-{
		$wnd.Y.all('iframe.ke-iframe').each(function(node){
			node.show();
		});
	}-*/;
	
	public  final native void fetchScript(String url)/*-{
		var script = $doc.createElement("script");
	   	script.setAttribute("src", url);
	   	script.setAttribute("type", "text/javascript");
	   	$doc.body.appendChild(script);
	}-*/;
	
	public  final native String getKeContent(String id)/*-{
		return $wnd.KE.html(id);
	}-*/;
	
	public  final native void setKeContent(String id,String content)/*-{
		$wnd.Y.log(id);
		$wnd.Y.log(content);
		$wnd.KE.html(id,content);
	}-*/;
	
	
	
	public  final native void redirect(String url)/*-{
		$wnd.location = url;
	}-*/;
	
	public  final native void reload()/*-{
		$wnd.location.reload();
	}-*/;
	
	
	public  final native void redirectToLogin()/*-{
		if($wnd.location.href.indexOf("codesvr") == -1){
			$wnd.location = "/Login.html";
		}else{
			$wnd.location = "http://127.0.0.1:8888/Login.html?gwt.codesvr=127.0.0.1:9997";
		}
	}-*/;
	
	public  final native void redirectToHomePage()/*-{
		if($wnd.location.href.indexOf("codesvr") == -1){
			$wnd.location = "/";
		}else{
			$wnd.location = "http://127.0.0.1:8888/?gwt.codesvr=127.0.0.1:9997";
		}
	}-*/;
	
	public  final native String getTimeZone()/*-{
		var timezone = $wnd.jzTimezoneDetector.determine_timezone().timezone;
		return timezone.olson_tz;
	}-*/;
}
